package com.woorigil.android.koreaflower.activity.product;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

import com.woorigil.android.koreaflower.activity.R;
import com.woorigil.android.koreaflower.domain.product.Product;
import com.woorigil.android.koreaflower.support.DisplayUtils;
import com.woorigil.android.koreaflower.support.StringUtils;

@SuppressLint("HandlerLeak") 
public class ProductDetailActivity extends Activity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product_detail_activity);
        
		WebView image = (WebView)findViewById(R.id.deal_image);
		image.setFocusable(false);
		LayoutParams layoutParams = image.getLayoutParams();
		int width = DisplayUtils.widthPixels(this);
		layoutParams.width = width;
		layoutParams.height = (int)(width * 0.65);
		image.setLayoutParams(layoutParams);
		
		TextView title = (TextView) findViewById(R.id.detail_title);
		TextView originalPrice = (TextView) findViewById(R.id.detail_original_price);
		TextView salesPrice = (TextView) findViewById(R.id.detail_sales_price);
		TextView discountRate = (TextView) findViewById(R.id.detail_discount_rate);
		TextView nowCount = (TextView) findViewById(R.id.detail_now_count);
		TextView saleToDate = (TextView) findViewById(R.id.detail_sale_to_date);
		TextView providerName = (TextView) findViewById(R.id.detail_provider_name);
					
		final Product product = (Product)getIntent().getSerializableExtra("product");
		
		if (image != null) {
			StringBuffer sb = new StringBuffer();
			sb.append("<html><body style='padding:0;margin:0;'>");
			sb.append("<img src='" + product.getThumbnailImageUrl2() + "' style='width:100%;padding:0;margin:0' />");
			sb.append("</body></html>");
			image.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);
		}

		
		title.setText(product.display1());
		originalPrice.setText(DisplayUtils.numFormat(product.getOriginalPrice()) + "원");
		originalPrice.setPaintFlags(originalPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
		salesPrice.setText(DisplayUtils.numFormat(product.getSalesPrice()) + "원");
		discountRate.setText(product.getDiscountRate() + "%");
		nowCount.setText(DisplayUtils.numFormat(product.getNowCount()) + "개");
		saleToDate.setText(DisplayUtils.dateFormat(Long.parseLong(product.getToDate())) + " 까지");
		
		Button loveButton = (Button)findViewById(R.id.love_button);
		loveButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
//        		Uri uri = Uri.parse(product.getDetailViewUrl());
//        		startActivity(new Intent(Intent.ACTION_VIEW, uri));
        	}
        });   
        Button detailButton = (Button)findViewById(R.id.detail_button);
        detailButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		Uri uri = Uri.parse(product.getDetailViewUrl());
        		startActivity(new Intent(Intent.ACTION_VIEW, uri));
        	}
        });        

    }
}
